<template>
  <div id="profile">
    <div v-if="!user" class="not_login user_bj">
      <img @click="$router.push('/login')" src="@/assets/mobile.png" alt="" />
      <span>登录 / 注册</span>
    </div>
    <div v-else class="login_box user_bj">
      <div class="user_info_t">
        <div>
          <van-image round fit="cover" :src="users.photo" />
          <span class="text">{{ users.name }}</span>
        </div>
        <van-button type="default" to="/user">编辑资料</van-button>
      </div>
      <div class="user_info_b">
        <div class="data_item">
          <span>{{ users.art_count }}</span>
          <span>头条</span>
        </div>
        <div class="data_item">
          <span>{{ users.follow_count }}</span>
          <span>关注</span>
        </div>
        <div class="data_item">
          <span>{{ users.fans_count }}</span>
          <span>粉丝</span>
        </div>
        <div class="data_item">
          <span>{{ users.like_count }}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>
    <van-grid clickable :column-num="2">
      <van-grid-item icon="home-o" text="收藏" to="shoucang">
        <template #icon>
          <i class="toutiao toutiao-shoucang"></i>
        </template>
      </van-grid-item>
      <van-grid-item icon="search" text="历史" to="/lishi">
        <template #icon> <i class="toutiao toutiao-lishi"></i> </template
      ></van-grid-item>
    </van-grid>

    <div class="marginBox">
      <van-cell title="消息通知" is-link />
      <van-cell title="小智同学" is-link />
    </div>
    <van-cell title="退出" @click="logout" class="logout" v-if="user" />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { Toast, Dialog } from 'vant'
import { getUserInfo } from '@/api/user.js'
export default {
  data () {
    return {
      users: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created () {
    if (this.user) {
      this.loadUserInfo()
    }
  },
  // 定义方法
  methods: {
    logout () {
      Dialog.confirm({
        title: '黑马头条',
        message: '确认退出？'
      })
        .then(() => {
          this.$store.commit('setUser', null)
          Toast('已退出')
        })
        .catch(() => {})
    },
    async loadUserInfo () {
      try {
        const { data: res } = await getUserInfo()
        console.log(res)
        this.users = res.data
        Toast.clear()
      } catch (err) {
        Toast('系统异常')
      }
    }
  }
}
</script>

<style scoped lang="less">
#profile {
  .user_bj {
    width: 100%;
    height: 401px;
    background-image: url(~@/assets/banner.png);
    background-size: 100%;
  }
  .not_login {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    img {
      width: 132px;
      height: 132px;
    }
    span {
      font-size: 28px;
      color: #fff;
      margin-top: 15px;
    }
  }
  .login_box {
    overflow: hidden;
    .user_info_t {
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 132px;
      margin-top: 116px;
      padding: 0 32px;
      .van-image {
        width: 132px;
        height: 132px;
        vertical-align: middle;
      }
      .text {
        color: #fff;
        font-size: 30px;
        margin-left: 22px;
      }
      .van-button {
        width: 115px;
        height: 32px;
        font-size: 20px;
        color: #666;
        padding: 0;
        border-radius: 16px;
      }
    }
    .user_info_b {
      display: flex;
      justify-content: space-around;
      align-items: center;
      height: 153px;
      .data_item {
        display: flex;
        flex-direction: column;
        align-items: center;
        color: #fff;
        font-size: 23px;
        span:nth-child(1) {
          font-size: 26px;
          margin-bottom: 15px;
        }
      }
    }
  }
  .van-grid {
    .toutiao {
      font-size: 45px;
    }
    .toutiao-shoucang {
      color: #eb5253;
    }
    .toutiao-lishi {
      color: #ff9d1d;
    }
    /deep/ .van-grid-item__text {
      font-size: 28px;
      color: #333;
      margin-top: 10px;
      letter-spacing: 20px;
      padding-left: 20px;
    }
  }
  .marginBox {
    margin: 10px 0;
  }
  .logout {
    color: #d86262;
    font-size: 29px;
    text-align: center;
  }
}
</style>
